/*
 * TaskListMenu.java
 *
 * Created on 23.06.2008, 22:23
 *
 * Copyright (c) 2006-2008, Daniel Apatin (ad), http://apatin.net.ru
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * You can also redistribute and/or modify this program under the
 * terms of the Psi License, specified in the accompanied COPYING
 * file, as published by the Psi Project; either dated January 1st,
 * 2005, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package Menu;

import Tasks.TaskEdit;
import Tasks.TaskList;
import Tasks.Tasks;
import images.RosterIcons;
import io.file.FileIO;
import io.file.browse.Browser;
import io.file.browse.BrowserListener;
import javax.microedition.lcdui.Display;
import midlet.FMSE;
import midlet.StaticData;

/**
 *
 * @author ad
 */
public class TaskListMenu
        extends Menu {
    
    Display display;
    TaskList tasklist;
    Tasks tasks;
    int curPos;
    
    /** Creates a new instance of TaskListMenu */
    public TaskListMenu(Display display, TaskList tasklist, int curPos) {
        super("", RosterIcons.getInstance());
        this.display=display;
        this.tasklist=tasklist;
        this.curPos=curPos;
        
        parentView=display.getCurrent();
        
        tasks=Tasks.getInstance();
        
        addItem("Add new", 0);
        if (tasks.size()>0) {
            addItem("Edit", 1);
            addItem("Save", 2);
            addItem("Delete", 4);
            if (tasks.size()>1)
                addItem("Delete all", 5);
        }
        addItem("Load", 3);
                
        addItem("Quit", 99);
        
	attachDisplay(display);
    }
    
    
    public void eventOk(){
	MenuItem me=(MenuItem) getFocusedObject();
        
	if (me==null)  return;
        
	int index=me.index;
	switch (index) {
	    case 0: //Add new
                destroyView();
                new TaskEdit(display, null, tasklist);
                break;
	    case 1: //Edit
                destroyView();
                new TaskEdit(display, tasks.get(curPos), tasklist);
		break;
	    case 2: //Save
                //display.setCurrent(StaticData.getInstance().tasklist);
                //tasklist.whattodo=2;
                //new Browser(null, display, tasklist, false);
                saveData ();
                break;
	    case 3: //Load
                //display.setCurrent(StaticData.getInstance().tasklist);
                tasklist.whattodo=3;
                new Browser(null, display, tasklist, false);
		break;
            case 4: //delete
                destroyView();
                tasks.delete(curPos);
                break;
            case 5: //delete all
                destroyView();
                tasks.deleteAll();
                break;
            case 99: //Quit
                FMSE.getInstance().notifyDestroyed();
        }
    }
    
    private void saveData () {
        String file="c:/other/default_upgrade.xml";

        byte[] bodyMessage=tasks.getXml().getBytes();

        FileIO fileIO=FileIO.createConnection(file);
        fileIO.fileWrite(bodyMessage);
    }
    
    public void destroyView()	{
        if (display!=null) display.setCurrent(parentView);
    }
}
